<?php

// +----------------------------------------------------------------------
// | Shop-Demo for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2022~2023 Anyon <zoujingli@qq.com>
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// | 会员免费 ( https://thinkadmin.top/vip-introduce )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\data\controller\daikuan;

use app\data\model\LoansApplication as DataNewsItem;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\admin\helper\QueryHelper;
use think\facade\Db;

/**
 * 贷款申请列表
 * Class Item
 * @package app\data\controller\news
 */
class LoansApplications extends Controller
{
    /**
     * 贷款申请列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->type = $this->get['type'] ?? 'index';
        DataNewsItem::mQuery($this->get)->layTable(function () {
            $this->title = '贷款申请列表';
        }, function (QueryHelper $query) {
            $query->like('institutions_name')->dateBetween('create_at');
            $query->where(['status' => intval($this->type === 'index'), 'deleted' => 0]);
        });
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _page_filter(array &$data)
    {

        foreach ($data as &$item){

            if (!empty($item['user_id'])) {
                $item['user_name'] = Db::name("data_user")->where('id', $item['user_id'])->value('nickname');
            } else {
                $item['user_name'] = '';
            }


            if (!empty($item['loans_id'])) {
                $item['loans_name'] = Db::name("loans")->where('id', $item['loans_id'])->value('loans_name');
            } else {
                $item['loans_name'] = '';
            }

        }
    }

    /**
     * 添加文章内容
     */
    public function add()
    {
        $this->title = '添加内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 编辑文章内容
     */
    public function edit()
    {
        $this->title = '编辑内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {

        if ($this->request->isGet()) {

        } else {

        }
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state)
    {
        if ($state) {
            $this->success('保存成功！', 'javascript:history.back()');
        }
    }

    /**
     * 修改文章状态
     */
    public function state()
    {
        DataNewsItem::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除文章内容
     */
    public function remove()
    {
        DataNewsItem::mDelete();
    }

    /**
     * 文章内容选择
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function select()
    {
        $this->get['status'] = 1;
        $this->index();
    }


//
//    /**
//     * 审核驳回
//     * @auth true
//     */
//    public function shbh()
//    {
//        DataNewsItem::mForm('shbh');
//    }
//
//
//    protected function _shbh_form_filter(array &$data)
//    {
//
//        if ($this->request->isPost()) {
//
//            $data['sh_status'] = 9;
//        }
//    }
//
//
//
//    /**
//     * 审核通过
//     * @auth true
//     */
//    public function shtg()
//    {
//        $id = input('id');
//
//        $info = Db::name("loans_application")->where('id',$id)->find();
//
//        if(!empty($info)){
//            Db::name("loans")->where('id',$info['loans_id'])->inc('apply_num')->save();
//            $this->success('操作成功！', 'javascript:history.back()');
//        }else{
//            $this->success('审核失败！', 'javascript:history.back()');
//        }
//
//
//
//    }

}